'use strict';
const db = uniCloud.database()
const dbCmd = db.command
exports.main = async (event, context) => {
	//event为客户端上传的参数
	const { user_id, article_id } = event;
	
	const user = await db.collection("user").doc(user_id).get()
	const thumbs_up_article_ids = user.data[0].thumbs_up_article_ids
	let thumbs_ids = null, inc = null;
	// console.log(thumbs_up_article_ids, article_id, "article_idarticle_idarticle_id");
	if(thumbs_up_article_ids.includes(article_id)){
		thumbs_ids = dbCmd.pull(user_id)
		inc = dbCmd.inc(-1)
	}else{
		thumbs_ids = dbCmd.addToSet(article_id)
		inc = dbCmd.inc(1)
	}
	
	const thumbs_ids_list = await db.collection('user').doc(user_id).update({
		thumbs_up_article_ids: thumbs_ids
	})
	
	await  db.collection('article').doc(article_id).update({
		thumbs_up_count: inc
	})
	
	
	//返回数据给客户端
	return  {
		status: 200,
		msg: '点赞成功',
		data:thumbs_ids_list
		}
};
